<template>
  <div class="app-container">
    <el-card style="border-radius: 10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="收款单编码" prop="moneyinCode">
        <el-input
          v-model="queryParams.moneyinCode"
          placeholder="请输入收款单编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="organName">
        <el-input
          v-model="queryParams.organName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户编码" prop="organCode">
        <el-input
          v-model="queryParams.organCode"
          placeholder="请输入客户编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经手人名称" prop="handsPersonName">
        <el-input
          v-model="queryParams.handsPersonName"
          placeholder="请输入经手人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售订单编号" prop="billCode">
        <el-input
          v-model="queryParams.billCode"
          placeholder="请输入销售订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据日期" prop="billTime">
        <el-date-picker clearable
          v-model="queryParams.billTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择单据日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>
    <div style="height: 15px"></div>
    <el-card style="border-radius: 10px">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['financial:moneyin:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="moneyinList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="收款单编码" align="center" prop="moneyinCode" :show-overflow-tooltip="true"/>
      <el-table-column label="销售单编码" align="center" prop="billCode" :show-overflow-tooltip="true"/>
      <el-table-column label="客户编码" align="center" prop="organCode" :show-overflow-tooltip="true"/>
      <el-table-column label="客户名称" align="center" prop="organName" :show-overflow-tooltip="true"/>
      <el-table-column label="合计金额" align="center" prop="totalPrice">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.totalPrice }}</el-tag>
        </template>
      </el-table-column>
<!--      <el-table-column label="优惠金额" align="center" prop="discountMoney"/>-->
      <el-table-column label="已收金额" align="center" prop="changeAmount">
        <template slot-scope="scope">
          <el-tag type="success">{{ scope.row.changeAmount }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="欠款金额" align="center" prop="changeAmount">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.totalPrice!==scope.row.changeAmount">{{ scope.row.totalPrice - scope.row.changeAmount }}</el-tag>
          <el-tag type="success" v-else>{{ scope.row.totalPrice - scope.row.changeAmount }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="单据日期" align="center" prop="billTime" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.billTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.status==='待收款'">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-tooltip content="收款" placement="top" v-if="scope.row.status === '待收款'">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-video-play"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['financial:moneyin:edit']"
          ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>
    <!-- 添加或修改收入单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-descriptions :column="2" class="is-bordered" style="margin-bottom: 20px;" border>
        <el-descriptions-item>
          <template slot="label">
            收款单编号
          </template>
          {{ form.moneyinCode }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            单据日期
          </template>
          {{ form.billTime }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            销售单编号
          </template>
          {{ form.billCode }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            客户编号
          </template>
          {{ form.organCode }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            合计金额
          </template>
          <el-tag >{{ form.totalPrice }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            客户名称
          </template>
          {{ form.organName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            已收金额
          </template>
          <el-tag  type="success">{{ form.changeAmount }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            销售人员
          </template>
          {{ form.handsPersonName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            欠款金额
          </template>
          <el-tag  type="danger">{{ form.debtMoney }}</el-tag>
        </el-descriptions-item>
      </el-descriptions>
      <el-divider content-position="center">收款单据</el-divider>
      <el-card shadow="always" class="box-card">

      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMoneyin, getMoneyin, addMoneyin, updateMoneyin } from "@/api/mes/financial/moneyin";
import {parseTime} from "../../../../utils/ruoyi";

export default {
  name: "Moneyin",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 收入单表格数据
      moneyinList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        moneyinCode: null,        type: null,        organId: null,        organName: null,        organCode: null,        handsPersonId: null,        handsPersonName: null,        changeAmount: null,        discountMoney: null,        totalPrice: null,        accountId: null,        billCode: null,        billTime: null,        fileName: null,        status: null,        source: null,        tenantId: null,        deleteFlag: null,        attr1: null,        attr2: null,        attr3: null,        attr4: null      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    parseTime,
    /** 查询收入单列表 */
    getList() {
      this.loading = true;
      listMoneyin(this.queryParams).then(response => {
        this.moneyinList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,        moneyinCode: null,        type: null,        organId: null,        organName: null,        organCode: null,        handsPersonId: null,        handsPersonName: null,        changeAmount: null,        discountMoney: null,        totalPrice: null,        accountId: null,        billCode: null,        billTime: null,        remark: null,        fileName: null,        status: "0",        source: null,        tenantId: null,        deleteFlag: null,        createBy: null,        createTime: null,        updateBy: null,        updateTime: null,        attr1: null,        attr2: null,        attr3: null,        attr4: null      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMoneyin(id).then(response => {
        this.form = response.data;
        this.form.debtMoney = this.form.totalPrice - this.form.changeAmount;
        this.open = true;
        this.title = "收款";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMoneyin(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMoneyin(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('financial/moneyin/export', {
        ...this.queryParams
      }, `moneyin_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>

</style>
